;
(function (window, angular) {
    'use strict';
    angular.module("Controller.Report.OutBoundSummary", [])
        .controller("OutBoundSummaryCtrl", ["$scope", "$state", "ybWidget", "ybHttp", "MESG_TYPE", "$filter", "$timeout", "UserContextService", "$stateParams", function ($scope, $state, ybWidget, ybHttp, MESG_TYPE, $filter, $timeout, UserContextService, $stateParams) {
            var btnPermissionCode = {
                saleOrderExport: window.ActionCode.saleOrderExport,
            };

            $scope.btnPermission = {
                saleOrderExport: true,
            };

            function setBtnPermission() {
                for (var key in btnPermissionCode) {
                    $scope.btnPermission[key] = UserContextService.CheckPermission([btnPermissionCode[key]]);
                }
            }

            $scope.markInfo = {
                curRow: -1
            };

            $scope.pageInfo = {
                totalPages: 1,
                totalRows: 0,
                pageSize: 10,
                curPage: 1
            };

            $scope.exportParam = {
                allUrl: ybHttp.RequestUrl("device", "/outboundSummary/export")
            };

            $scope.toolbarConfig = {
                showAdvanceSearch: false,
                showColConfig: false,
            };
            $scope.switchToolbar = function (toolbar) {
                if (!!$scope.toolbarConfig["show" + toolbar]) {
                    $scope.toolbarConfig["show" + toolbar] = false;
                } else {
                    $timeout(function () {
                        $scope.toolbarConfig["show" + toolbar] = true;
                    });
                }
            };

            $scope.condition = {};
            $scope.dataList = [];

            $scope.tbConf = {
                scrollWidth: 4600,
                tableCol: [
                    {
                        name: "NC客户编码/供货商代码",
                        dataField: "customNcCode",
                        width: 170,
                        isShow: true
                    },
                    {
                        name: "PMS供货商代码/客户代码",
                        dataField: "customPmsCode",
                        width: 170,
                        isShow: true
                    },
                    {
                        name: "客户名称/供货商名称",
                        dataField: "customName",
                        width: 170,
                        isShow: true
                    },
                    {
                        name: "客户/供货商类别",
                        dataField: "customType",
                        width: 170,
                        isShow: true
                    },
                    {
                        name: "出库单号",
                        dataField: "outBillNo",
                        width: 170,
                        isShow: true
                    },
                    {
                        name: "出库类别",
                        dataField: "outType",
                        width: 170,
                        isShow: true
                    },
                    {
                        name: "销售订单",
                        dataField: "sellBillNo",
                        width: 170,
                        isShow: true
                    },
                    {
                        name: "采购退货单号",
                        dataField: "returnBillNo",
                        width: 170,
                        isShow: true
                    },
                    {
                        name: "三包备件指令单号",
                        dataField: "orderBillNo",
                        width: 170,
                        isShow: true
                    },
                    {
                        name: "预留单号",
                        dataField: "reservedBillNo",
                        width: 170,
                        isShow: true
                    },
                    {
                        name: "仓库",
                        dataField: "warehouseCode",
                        width: 170,
                        isShow: true
                    },
                    {
                        name: "库区",
                        dataField: "areaCode",
                        width: 170,
                        isShow: true
                    },
                    {
                        name: "制单人",
                        dataField: "createName",
                        width: 170,
                        isShow: true
                    },
                    {
                        name: "创建时间",
                        dataField: "createTimeShow",
                        width: 160,
                        isShow: true
                    },
                    {
                        name: "出库时间",
                        dataField: "outTimeShow",
                        width: 160,
                        isShow: true
                    },
                    {
                        name: "零件编号",
                        dataField: "materialsNo",
                        width: 170,
                        isShow: true
                    },
                    {
                        name: "零件中文名",
                        dataField: "materialsDesc",
                        width: 200,
                        isShow: true
                    },
                    {
                        name: "计量单位",
                        dataField: "unit",
                        width: 170,
                        isShow: true
                    },
                    {
                        name: "数量",
                        dataField: "quantity",
                        width: 170,
                        isShow: true
                    },
                    {
                        name: "单价",
                        dataField: "price",
                        width: 170,
                        isShow: true
                    },
                    {
                        name: "销售总额",
                        dataField: "sellAmount",
                        width: 170,
                        isShow: true
                    },
                    {
                        name: "实际开票金额（含税）",
                        dataField: "billAmountWithTax",
                        width: 170,
                        isShow: true
                    },
                    {
                        name: "实际开票金额（不含税）",
                        dataField: "billAmountWithoutTax",
                        width: 170,
                        isShow: true
                    },
                    {
                        name: "进货基准价",
                        dataField: "flatPrice",
                        width: 170,
                        isShow: true
                    },
                    {
                        name: "未税成本价",
                        dataField: "costPriceWithoutTax",
                        width: 170,
                        isShow: true
                    },
                    {
                        name: "含税成本价",
                        dataField: "costPriceWithTax",
                        width: 170,
                        isShow: true
                    },
                    {
                        name: "备注",
                        dataField: "remark",
                        width: 170,
                        isShow: true
                    },
                ]
            };

            $scope.reset = function () {
                $scope.condition = {};
            };

            $scope.search = function (curpage) {
                getDataList(curpage);
            };

            $scope.selectRow = function (index) {
                $scope.markInfo.curRow = index;
            };

            $scope.downloadDisabled = true;
			$scope.checkDate = function (obj, startFiled, endFiled) {
					var timeFormatObj = ybWidget.CheckDate(obj[startFiled], obj[endFiled]);
					if (!!timeFormatObj.tStart) {
						obj[startFiled] = timeFormatObj.tStart;
					} else {
						$scope.downloadDisabled = true;
					}
                    
					if (!!timeFormatObj.tEnd) {
						if (!!timeFormatObj.tStart) {
                            var diffDay = Math.ceil((timeFormatObj.tEnd.getTime()-timeFormatObj.tStart.getTime())/(1000*24*60*60));
                            if (diffDay>31) {
                                $scope.downloadDisabled = true;
                                ybWidget.ShowToaster('error', '导出记录时，时间跨度不能超过31天(当前跨度为'+diffDay+'天)');
                            } else {
                                $scope.downloadDisabled = false;
                            }
						}
                        obj[endFiled] = timeFormatObj.tEnd;
					} else {
						$scope.downloadDisabled = true;
					}
			};

            function getDataList(curPage) {
                $scope.markInfo.curRow = -1;
                if (!!curPage) {
                    $scope.pageInfo.curPage = curPage;
                }

                var service = "device";
                var reqUrl = "/outboundSummary/pageList";
                var param = {
                    pageNum: $scope.pageInfo.curPage,
                    pageSize: $scope.pageInfo.pageSize,
                    filters: angular.copy($scope.condition)
                };
                ybHttp.Post(service, reqUrl, param).then(function (resp) {
                    if (resp.code !== 0) {
                        ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                        return;
                    }
                    angular.forEach(resp.value.list, function (item) {
                        item.createTimeShow = $filter("YbDate")(item.createTime, 'yyyy-MM-dd HH:mm:ss');
                        item.outTimeShow = $filter("YbDate")(item.outTime, 'yyyy-MM-dd HH:mm:ss');
                    });
                    $scope.pageInfo.totalRows = resp.value.total;
                    $scope.pageInfo.totalPages = resp.value.pages;
                    $scope.dataList = resp.value.list;
                });
            }

            (function () {
                UserContextService.SetBtnPermission(setBtnPermission);
            })();
        }])
    ;
})(window, window.angular);